from django.urls import path, reverse

from supergirl.models import Customer
from gideon.service.starlabs import GideonHandler


class PrivateBaseHandler(GideonHandler):
    def get_urls(self):
        patterns = [
            # 可以使用面板或者时间树的形式进行展示
            path(r'list/<slug:customer_key>/', self.handler_view_list, name=self.get_url_name_list()),
            path(r'add/<slug:customer_key>/', self.handler_view_add, name=self.get_url_name_add()),
            path(r'change/<slug:key>/<slug:customer_key>/', self.handler_view_change, name=self.get_url_name_change()),
            path(r'delete/<slug:key>/<slug:customer_key>/', self.handler_view_delete, name=self.get_url_name_delete()),
        ]
        patterns.extend(self.get_extra_urls())
        return patterns

    def get_queryset(self, handle_type='list', *args, **kwargs):
        customer_key = kwargs.get("customer_key", "")
        # 不考虑多用户的情况
        current_user_id = self.request.session.get("oracle_users_list")
        # 当前用户id
        if current_user_id:
            current_user_id = current_user_id[0]["id"]
        else:
            current_user_id = None
        if customer_key:
            # customer__key 客户信息  customer__consultant_id 客户对应的课程顾问信息
            queryset = self.model_class.objects.filter(customer__key=customer_key,
                                                       customer__consultant_id=current_user_id)
        else:
            queryset = self.model_class.objects.filter(customer__key=None)
        if handle_type == "change" or handle_type == "delete":
            key = kwargs.get('key')
            return queryset.filter(key=key).first()
        else:
            return queryset

    def reverse_url_add(self, request, *args, **kwargs):
        """
        生成带原搜索条件的添加url
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        customer_key = kwargs.get("customer_key", "")
        return self.reverse_handle_url(request, self.get_url_name_add(), customer_key=customer_key)

    def reverse_url_change(self, request, obj, *args, **kwargs):
        """
        生成带原搜索条件的修改url
        :param request:
        :param obj:
        :param args:
        :param kwargs:
        :return:
        """
        customer_key = kwargs.get("customer_key", "")
        key = obj.key
        return self.reverse_handle_url(request, self.get_url_name_change(), key=key, customer_key=customer_key)

    def reverse_url_delete(self, request, obj, *args, **kwargs):
        """
        生成带原搜索条件的删除url
        :param request:
        :param args:
        :param obj:
        :param kwargs:
        :return:
        """
        customer_key = kwargs.get("customer_key", "")
        key = obj.key
        return self.reverse_handle_url(request, self.get_url_name_delete(), key=key, customer_key=customer_key)

    def reverse_url_list(self, request, *args, **kwargs):
        """
        跳转回页面时生成的url
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        customer_key = kwargs.get("customer_key", "")
        name = f'{self.site.namespace}:{self.get_url_name_list()}'
        list_url = reverse(name, kwargs={"customer_key": customer_key, })
        params = request.GET.get('_filter')
        if params:
            list_url = f'{list_url}?{params}'
        return list_url

    def form_handler(self, form, is_update=False, *args, **kwargs):
        pass

    def saving_pre_handler(self, form, is_update=False, *args, **kwargs):
        current_user_id = self.request.session.get("oracle_users_list")
        if current_user_id:
            current_user_id = current_user_id[0]["id"]
            customer_key = kwargs.get("customer_key", "")
            if not Customer.objects.filter(key=customer_key, consultant_id=current_user_id).exists():
                return False
        else:
            return False
        if not is_update:
            form.instance.consultant_id = current_user_id
            form.instance.customer_id = Customer.objects.filter(key=customer_key).first().id
        self.form_handler(form, is_update, *args, **kwargs)
        return super().saving_pre_handler(form, is_update=False, *args, **kwargs)
